package com.bdsy.system.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.Result;
import com.bdsy.common.utils.UUIDUtil;
import com.bdsy.system.annotation.SysLog;
import com.bdsy.system.model.AuthTree;
import com.bdsy.system.model.Authorization;
import com.bdsy.system.model.Role;
import com.bdsy.system.service.AuthorizationService;
import com.bdsy.system.service.RoleAuthService;
import com.bdsy.system.service.RoleService;

/**
 * ClassName:UserController Description: TODO 添加描述.
 * 
 * @author Administrator
 * @version
 * @since JDK 1.8
 */
@Controller
@RequestMapping(value = "${web.adminPath}")
public class RoleController {

	@Autowired
	RoleService roleService;

	@Autowired
	RoleAuthService roleAuthService;

	@Autowired
	AuthorizationService authorizationService;

	@RequestMapping(value = "sysrole")
	public String sysrole() {
		return "admin/system/sysrole";
	}
	
	@Json
	@RequestMapping(value = "sysroleList")
	public Object sysroleList() {
		List<Role> list = roleService.getRole();
		return list;
	}

	@Json
	@RequestMapping(value = "listTree")
	public Object listMenuTree(String roleId, HttpServletRequest request) {
		List<AuthTree> listMenuTree = new ArrayList<AuthTree>();
		List<Authorization> list = authorizationService.getAuthorization();
		int count = this.roleAuthService.getRoleAuthCount(roleId);
		if (count <= 0) {
			if (list != null && list.size() > 0) {
				for (Authorization m : list) {
					AuthTree authTree = new AuthTree();
					authTree.setId(m.getAuthId());
					authTree.setName(m.getAuthName());
					authTree.setOpen(false);
					authTree.setpId(m.getAuthParentId());
					listMenuTree.add(authTree);
				}
			}
			return listMenuTree;
		} else {
			List<Authorization> list1 = authorizationService
					.getCheckedAuthByRole(roleId);
			if (list != null && list.size() > 0 && list1 != null
					&& list1.size() > 0) {
				for (Authorization m : list) {
					AuthTree authTree = new AuthTree();
					authTree.setId(m.getAuthId());
					authTree.setName(m.getAuthName());
					authTree.setOpen(true);
					authTree.setpId(m.getAuthParentId());
					if (list1.contains(m)) {
						authTree.setChecked(true);
					} else {
						authTree.setChecked(false);
					}
					listMenuTree.add(authTree);
				}
			}
			return listMenuTree;
		}
	}

	@Json
	@SysLog("新增角色权限")
	@RequestMapping(value = "addRoleAuth", method = { RequestMethod.GET,RequestMethod.POST })
	public Object addRoleAuth(String authId, String roleId,
			HttpServletRequest request) {
		if (authId == null || authId == "") {
			int count = this.roleAuthService.getRoleAuthCount(roleId);
			if (count > 0) {
				this.roleAuthService.deleteRoleAuth(roleId);
			}
			return Result.error("添加角色和权限菜单失败");
		} else {
			int count = this.roleAuthService.getRoleAuthCount(roleId);
			if (count > 0) {
				this.roleAuthService.deleteRoleAuth(roleId);
			}
			this.roleAuthService.addRoleAuth(roleId, authId);
			return Result.ok("角色权限修改成功");
		}
	}

	@Json
	@SysLog("新增角色")
	@RequiresPermissions("sys:role:save")
	@RequestMapping(value = "addRole")
	public Object addRole(HttpServletRequest request, Role role) {
		role.setRoleId(UUIDUtil.randomUUID());
		role.setCreateDate(new Date());
		boolean result = roleService.addRole(role);
		
		if (result) {
			return Result.ok("添加成功");
		} else {
			return Result.error("添加失败");
		}
	}

	@Json
	@SysLog("修改角色")
	@RequestMapping(value = "updateRole")
	@RequiresPermissions("sys:role:update")
	public Object updateRole(HttpServletRequest request, Role role) {

		boolean result = roleService.updateRole(role);
		if (result) {
			return Result.ok("修改成功");
		} else {
			return Result.error("修改失败");
		}
	}

	/**
	 * 验证角色名称是否已存在 
	 */
	@Json
	@RequestMapping(value = "checkRoleName")
	public boolean checkRoleName(HttpServletRequest request, Role role) {
		Role r = roleService.checkRoleName(role);
		
		if (r == null) {
			return true;
		}
		return false;
	}

	/**
	 * 修改时，验证角色名称是否已存在一个
	 */
	@Json
	@RequestMapping(value = "checkUpdateRoleName")
	public boolean checkUpdateRoleName(HttpServletRequest request, Role role,String roleName1) {
		role.setRoleName(roleName1);
		Role r = roleService.checkUpdateRoleName(role);
		if (r == null) {
			return true;
		}
		return false;
	}
}
